var verificationPasswordState=false;
//更换验证码方法
function renewalVerificationCode(imgid,rl){
	var timestamp = Date.parse(new Date());
	var url=rl + "portal/captcha/generateCaptcha.do?"+timestamp;
	$('#'+imgid).attr("src",url);
};
//验证码码校验方法，需传入code
function verificationCodeAjax(code,errorname,imgid,rl){
    $.ajax({
		type:"get",
		url:rl+"portal/captcha/verifyCaptcha.json?captcha="+code,
		async:false,
		success:function(data){
			if(data.success){
			    if (data.data.isSuccess) {
			    	//更改短信校验状态
			    	$('.'+errorname).addClass("text-success").removeClass("text-danger").text(data.data.message);
			    	verificationCodeState=true;
			    	countDownNote();
			    	countDownRegister();
			    } else{
			    	//失败重新刷新
			    	$('.'+errorname).addClass("text-danger").removeClass("text-success").text(data.data.message);
			    	renewalVerificationCode(imgid,rl);
			    }
			}
		}
	});
}

//账号非重校验
function accountCheck(code,errorname,rl){
	$.ajax({
	type:"get",
	url:rl+"user/open/regist/verifyphone.json?mobilePhone="+code,
	async:true,
	success:function(data){
		if(data.success){
		    if (!data.data) {
		    	//成功则把发送短信按钮可用
		    	phonenumber=code;
		    	verificationPhoneState=true;
		    	$('.'+errorname).addClass("text-success").removeClass("text-danger").text("手机号校验通过");
		    } else{
		    	//失败提示
		    	$('.'+errorname).addClass("text-danger").removeClass("text-success").text("手机号已被使用");
		    }
		}
	}
	});
}
//短信验证码校验
function noteAffirm(captcha){
	$.ajax({
		type:"get",
		url:rl+"plugins/sms/open/validateSmsCaptcha.json?type=1&phonenumber="+phonenumber+"&captcha="+captcha,
		async:true,
		success:function(data){
			if(data.success){				
				if (data.data) {
					verificationNoteState=true;
					countDownRegister();
				}
				else{
					$('.note-message-state').addClass("text-danger").removeClass("text-success").text("验证码错误，请重新获取");
				}
			}
		}
	});
}
//获取安全秘钥
function gtbrief(rl){
	$.ajax({
		type:"get",
		url:rl+"open/safetykey/getkey.json",
		async:true,
		success:function(data){
			if(data.success){
				bskeyid=data.data;
			}
		}
	});
}
//发送短信
function sendnote(){
	$.ajax({
		type:"get",
		url:rl+"plugins/sms/open/sendCaptcha.json?type=1&phonenumber="+phonenumber+"&bskeyid="+bskeyid,
		async:true
	});
}
//发送短信按钮解锁
function countDownNote(){
	if(verificationCodeState&&verificationPhoneState){
		$('#noteBtn').removeAttr("disabled");
	}
};
//立即注册按钮解锁
function countDownRegister(){
	if(verificationPasswordState){
		if(verificationCodeState&&verificationPhoneState&&verificationNoteState&&verificationPasswordState){
			$('#submitBtn').removeAttr("disabled");
		}
		if(verificationCodeState&&verificationAaccountState&&verificationPasswordState){
			$('#AregisterBtn').removeAttr("disabled");
		}
	}
	
	if(verificationCodeState&&verificationPhoneState&&verificationNoteState){
		$('#phonesubmitBtn').removeAttr("disabled");
	}
}
//显示成功信息
function successMessage(messageid,time,message,remove,add){
	$('#'+messageid).text(message).removeClass(remove).addClass(add).fadeIn(time);
}
/*=============================================
 * 新增自定义验证方法
 =============================================*/
function addValidator(){
	//新增手机验证
	$.validator.addMethod("phoneValidator",function(value,element,params){
		var pattern = /^1[34578]\d{9}$/;
		return pattern.test(value);
	},"请输入正确的手机号");
	
	//长度为4验证
	$.validator.addMethod("lengthFour",function(value,element,params){
		return value.length == 4;
	},"长度不符");
	//密码强度：表示至少8个字符，必须同时含有大写字母和小写字母；或者同时有大写字母和小写字母;或者同时有小写字母和数字。以上三种情况满足一种即符合要求
	$.validator.addMethod("passwordValidator",function(value,element,params){
		var pattern = /^(?=.{8,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/;
		return pattern.test(value);
	},"请输入8-20位英文+数字，区分大小写");
}

$(function(){
	var countDown=60;//初始化一个定时器
	var timeDown;
	//发送短信验证码
	$('#noteBtn').click(function(){
		if($('#noteBtn').attr("disabled")=="disabled"){
			return
		}
		sendnote();
		$('#noteBtn').attr("disabled","disable");
		timeDown=setInterval(timer,1000);
		countDown=60
		function timer(){
			if(countDown<=0){
				$('#js-time-left').text("");
				clearInterval(timeDown);
				$('#noteBtn').removeAttr("disabled");
				return;
			}
			countDown--;
			$('#js-time-left').text(countDown+"s后,");
		}
	})	
});